package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
@Transactional
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper recordMapper;

    @Autowired
    private TbClueMapper tbClueMapper;

    @Override
    public int save(ClueTrackRecordVo tbClueTrackRecord) {
        String username = SecurityUtils.getUsername();
        List<ClueTrackRecordVo> search = search(tbClueTrackRecord.getClueId());
        TbClue tbClue = tbClueMapper.selectTbClueById(tbClueTrackRecord.getClueId());
        if(search.size()>0){
            // BeanUtils拷贝工具类，可以把一个对象拷贝到另一个对象上
            BeanUtils.copyProperties(tbClueTrackRecord,tbClue);
            tbClueMapper.updateTbClue(tbClue);
        }
        tbClueTrackRecord.setCreateBy(username);
        tbClueTrackRecord.setCreateTime(DateUtils.getNowDate());
        return recordMapper.save(tbClueTrackRecord);
    }


    @Override
    public List<ClueTrackRecordVo> search(Long clueId) {
        String username = SecurityUtils.getUsername();
        TbClue tbClue = tbClueMapper.selectTbClueById(clueId);
        List<ClueTrackRecordVo> recordVos = recordMapper.search(username, clueId);
        List<ClueTrackRecordVo> list = recordVos.stream().map((recordVo)->{
            BeanUtils.copyProperties(tbClue,recordVo);
            return recordVo;
        }).collect(Collectors.toList());
        return list;
    }
}
